extension :dist_patch => :link_scripts do
  set :_script_shebang, "#!/bin/sh"
  
  set :_link_script do
    script = [_script_shebang]

    #TODO: rethink DeploymentList concept - should probably look at FreeBSD port system
    (deployment_list.directories + deployment_list.files + deployment_list.links).each do |v|
      script << %Q(mkdir -p '#{File.dirname(v[1])}')
      script << %Q(ln -fhs "$1"/'#{v[0]}' '#{v[1]}')
    end

    script.join($/)
  end
  
  set :_unlink_script do
    script = [_script_shebang]

    (deployment_list.links + deployment_list.files + deployment_list.directories).each do |v|
      script << "unlink '#{v[1]}' || :"
    end

    script.join($/)
  end
  
  set :link_script_name, "link.sh"
  set(:link_script_path) { File.join(revision_build.patched_dist_path, link_script_name) }
  set :unlink_script_name, "unlink.sh"
  set(:unlink_script_path) { File.join(revision_build.patched_dist_path, unlink_script_name) }
  
  task :patch_dist do
    execute_previous

    open(link_script_path, "w") { |f| f.write _link_script }
    open(unlink_script_path, "w") { |f| f.write _unlink_script }
  end
end

